#define  _CRT_SECURE_NO_WARNINGS 
#include <iostream>

using namespace std;

//动物的抽象类
class Animal
{
public:
	virtual void cry() = 0;// 纯虚函数
	virtual void doHome() = 0;// 纯虚函数
};

class Dog :public Animal {
public:
	virtual void cry() {
		cout << "汪汪~~" << endl;
	}

	void doHome() {
		cout << "看家" << endl;
	}
};

class Cat :public Animal {
public:
	virtual void cry() {
		cout << "喵喵~~" << endl;
	}
	void doHome() {
		cout << "抓老鼠" << endl;
	}
};

void test1() {
	Animal* animal = NULL;
	animal = new Dog;
	animal->cry();
	Dog* dog = new Dog;
	cout << " ----- " << endl;
	dog = dynamic_cast<Dog*> (animal);
	if (dog != NULL) {
		cout << "转换成功" << endl;
		dog->cry();
		dog->doHome();
	}
	else {
		cout << "转换失败" << endl;
	}
	//dynamic _cast 是将父类转换成子类。让老子变成儿子。
	//让子类指针执行父类的对象。

	Cat* cat = dynamic_cast<Cat*>(animal);
	//准备试图让一只狗变成一只猫
	if (cat != NULL) {
		cout << "转换成功" << endl;
		cat->cry();
		cat->doHome();
	}
	else {
		cout << "转换失败" << endl;
		//cat->cry();
	}

	cout << " ----- " << endl;
}

class A{
public:
	virtual void func() {
		cout << "A" << endl;
	}
	int a;
};

class B :public A{
public:
	virtual void func() {
		cout << "B" << endl;
	}
	int b;
};

void test2() {
	A* ap = new A;	//父类指针
	B* bp = dynamic_cast<B*>(ap);	//因为有安全检查所以不能转换成功
	if (bp != NULL) {
		cout << "转换成功" << endl;
		bp->func();
		bp->b; //崩溃
	}
	else {
		cout << "转换失败" << endl;
	}

	cout << " -----" << endl;

}

int main(void)
{
	//test1();
	test2();
	return 0;
}
